package com.otdemo.service;

import com.otdemo.dao.RestClassificationDao;
import com.otdemo.entity.RestClassification;
import com.otdemo.resp.ResultResponse;

import java.util.List;
import java.util.stream.Collectors;

public class RestClassificationService {

    /**
     * 新增
     *
     * @return
     */
    public ResultResponse insert(RestClassification rest) {
        ResultResponse resultResponse = new ResultResponse();
        try {
            new RestClassificationDao().insert(rest);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
        return resultResponse.success();
    }

    /**
     * 更新
     *
     * @return
     */
    public ResultResponse update(RestClassification rest) {
        ResultResponse resultResponse = new ResultResponse();
        RestClassification rest1 = new RestClassificationDao().findByUid(rest.getUid());
        if (rest1 == null) {
            return resultResponse.fail("查询不存在");
        }
        try {
            new RestClassificationDao().update(rest);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
        return resultResponse.success();
    }

    /**
     * 查询
     *
     * @return
     */
    public ResultResponse findByUid(RestClassification rest) {
        ResultResponse resultResponse = new ResultResponse();
        try {
            RestClassification rest1 = new RestClassificationDao().findByUid(rest.getUid());
            return resultResponse.success(rest1);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
    }

    /**
     * 列表
     *
     * @return
     */
    public ResultResponse list(RestClassification rest) {
        ResultResponse resultResponse = new ResultResponse();
        try {
            List<RestClassification> list = new RestClassificationDao().list();
            return resultResponse.success(list);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
    }

    /**
     * 列表
     *
     * @return
     */
    public String[] classificationList() {
        try {
            List<RestClassification> list = new RestClassificationDao().list();
            List<String> names = list.stream().map(p -> p.getName()).collect(Collectors.toList());
            String[] strArray = new String[names.size() + 1];
            return names.toArray(strArray);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
